package com.hopu.blb_erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.entity.*;
import com.hopu.blb_erp.mapper.*;
import com.hopu.blb_erp.service.TRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TRelationServiceImpl extends ServiceImpl<TRelationMapper,TRelation> implements TRelationService {
    @Autowired
    private TRelationMapper tRelationMapper;
    @Autowired
    private TParMapper tParMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SCompanyMapper sCompanyMapper;

    @Transactional
    @Override
    public void addCustomer(TPar tPar) {
        tParMapper.updateById(tPar);
        SCompany company = tPar.getSCompany();
        Customer customer = new Customer();
        customer.setName(company.getCompanyName());
        customer.setTelephone(company.getFixedTel());
        customer.setEmail(company.getCompanyEmail());
        customer.setBusiness(company.getBusinessScope());
        customer.setAddress(company.getCompanyProvince() + company.getCompanyCity() + company.getCompanyArea() + company.getCompanyAddress());
        customerMapper.insert(customer);
//
//        org.springframework.security.core.userdetails.User user =
//                (org.springframework.security.core.userdetails.User)
//                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        User byName = userMapper.selectByName(user.getUsername());
//        SCompany sCompany = sCompanyMapper.selectById(byName.getCompanyId());
//        TRelation tRelation = new TRelation();
//        Customer customer1 = customerMapper.selectOne(new QueryWrapper<Customer>().eq("name", sCompany.getCompanyName()));
//        tRelation.setCustomerId(customer1.getId());
//        Supplier supplier = supplierMapper.selectOne(new QueryWrapper<Supplier>().eq("name", sCompany.getCompanyName()));
//        tRelation.setSupplierId(supplier.getId());
//        tRelationMapper.insert(tRelation);
    }

    @Transactional
    @Override
    public void addSupplier(TPar tPar) {
        tParMapper.updateById(tPar);
        SCompany company = tPar.getSCompany();
        Supplier supplier = new Supplier();
        supplier.setId(tPar.getTParComA());
        supplier.setName(company.getCompanyName());
        supplier.setTelephone(company.getFixedTel());
        supplier.setEmail(company.getCompanyEmail());
        supplier.setBusiness(company.getBusinessScope());
        supplier.setAddress(company.getCompanyProvince() + company.getCompanyCity() + company.getCompanyArea() + company.getCompanyAddress());
        supplierMapper.insert(supplier);
        //通过工具类的user的username查询user实体类的user
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User byName = userMapper.selectByName(user.getUsername());
        SCompany sCompany = sCompanyMapper.selectById(byName.getCompanyId());
        Supplier supplier1 = supplierMapper.selectOne(new QueryWrapper<Supplier>().eq("name", company.getCompanyName()));
        TRelation tRelation = new TRelation();
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().eq("name", sCompany.getCompanyName()));
        tRelation.setCustomerId(customer.getId());
        tRelation.setSupplierId(supplier1.getId());
        tRelationMapper.insert(tRelation);

    }
}
